home *** CD-ROM | disk | FTP | other *** search
/ PC Format Collection 21 / PC Format CD-ROM Collection 21 (1995-12)(Future Publishing)(GB)[issue 51].iso / resource / starfiel.poc < prev    next >
Text File  |  1990-10-24  |  3KB  |  168 lines

  1.  
  2. #define TRUE 1
  3. #define FALSE 0
  4.  
  5.     /* Z bounds */
  6. #define ZMAX 500.0    /* Farthest back things get */
  7. #define ZMIN (-100.0)        /* closest things get before perspective blows up */
  8. #define ZDEPTH (ZMAX-ZMIN)    /* length of total Z space */
  9. #define ZSMALL (ZMIN+1.0)    /* fudge factor to keep perspective in bounds */
  10.  
  11.     /* total number of colors */
  12. #define COLORS 256
  13.     /* Number of colors in ramp of a single star */
  14. #define ZCOLS 32
  15.     /* Number of different color ramps */
  16. #define ZRAMPS (COLORS/ZCOLS)
  17.  
  18.  
  19.  
  20.     /* number of stars */
  21. int scount = 333;
  22.     /* 3-D star positions */
  23. double *x, *y, *z;
  24.     /* Color of stars */
  25. char *color;
  26.  
  27.  
  28. int sw, sh;
  29. double cenx,ceny;
  30. int total_frames = 50;
  31.  
  32. make_ramp(int start, int colors, int rmax, int gmax, int bmax)
  33. {
  34. int i;
  35.  
  36. for (i=0; i<colors; ++i)
  37.     SetColorMap(start+i, rmax*i/colors, gmax*i/colors, bmax*i/colors);
  38. }
  39.  
  40. typedef struct {int r; int g; int b;} Rgb3;
  41.  
  42. Rgb3 rtab[ZRAMPS] =
  43.     {
  44.         {255, 255, 255},
  45.         {255, 200, 200},
  46.         {200, 255, 200},
  47.         {200, 200, 255},
  48.         {255, 100, 100},
  49.         {100, 100, 255},
  50.         {100, 255, 100},
  51.         {200, 200, 200},
  52.     };
  53.  
  54. init_colors()
  55. /* Make a bunch of color ramps */
  56. {
  57. int i;
  58.  
  59. for (i=0; i<ZRAMPS; ++i)
  60.     make_ramp(ZCOLS*i, ZCOLS, rtab[i].r, rtab[i].g, rtab[i].b);
  61. }
  62.  
  63. random_star(int starix)
  64. /* Initialize the data associated with a star index to a random value */
  65. {
  66. x[starix] = rnd(2000)-1000;
  67. y[starix] = rnd(2000)-1000;
  68. z[starix] = rnd(595)-95;
  69. color[starix] = rnd(ZRAMPS)*ZCOLS;
  70. }
  71.  
  72. init_stars(int count)
  73. /* Allocate star arrays and set all stars to a random positions and colors */
  74. {
  75. int i;
  76.  
  77. if ((x = malloc(count*sizeof *x)) == NULL)
  78.     goto ERROUT;
  79. if ((y = malloc(count*sizeof *y)) == NULL)
  80.     goto ERROUT;
  81. if ((z = malloc(count * sizeof *z)) == NULL)
  82.     goto ERROUT;
  83. if ((color = malloc(count * sizeof *color)) == NULL)
  84.     goto ERROUT;
  85. scount = count;
  86. for (i=0; i<scount; ++i)
  87.     random_star(i);
  88. return(TRUE);
  89. ERROUT:
  90. Qtext("Not enough memory for star arrays");
  91. return(FALSE);
  92. }
  93.  
  94. see_stars()
  95. /* Draw all the stars */
  96. {
  97. int i;
  98. double zscale,zz;
  99.  
  100. for (i=0; i<scount; ++i)
  101.     {
  102.     zz = z[i];        /* get Z coordinate */
  103.     if (zz > ZSMALL)
  104.         {
  105.             /* set color to match z */
  106.         SetColor(color[i]+(ZMAX-zz)*(ZCOLS/ZDEPTH)); 
  107.         zscale = -ZMIN/(zz-ZMIN);    /* find perspective shrink factor */
  108.         Dot(cenx+x[i]*zscale,ceny+y[i]*zscale); 
  109.         }
  110.     }
  111. }
  112.  
  113. move_stars()
  114. /* Move all the stars towards viewer */
  115. {
  116. int i;
  117. double dist = ZDEPTH/total_frames;
  118.  
  119. for (i=0; i<scount; ++i)
  120.     {
  121.     if ((z[i] = z[i]-dist) < ZMIN)
  122.         z[i] += ZDEPTH;
  123.     }
  124. }
  125.  
  126.  
  127. init()
  128. /* Do one time initializations */
  129. {
  130. GetSize(&sw,&sh);        /* find out screen size */
  131. cenx = sw/2;            /* and calculate screen center */
  132. ceny = sh/2;
  133. if (!init_stars(scount))
  134.     return(FALSE);            /* set up the arrays holding the stars */
  135. init_colors();            /* set up ramped color map */
  136. return(TRUE);
  137. }
  138.  
  139.  
  140.  
  141. main()
  142. {
  143. int i;
  144.  
  145. if (!Qnumber(&total_frames, 1, 200, "Number of frames for star-field"))
  146.     return;
  147. if (total_frames < 1)
  148.     return;
  149. if (SetFrameCount(1) < 0)
  150.     return;
  151. Clear();
  152. if (!Qnumber(&scount, 10, 1000, "Number of stars in field"))
  153.     return;
  154. if (scount < 1)
  155.     return;
  156. if (!init())
  157.     return;
  158. if (SetFrameCount(total_frames) < 0)
  159.     return;
  160. for (i=0;i<total_frames;++i)
  161.     {
  162.     see_stars();
  163.     move_stars();
  164.     NextFrame();
  165.     }
  166. }
  167.  
  168.